limsResult.auditPage = {

    getRunGrid: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'run-audit-list-grid',
            checkOnActive: true,
            service: 'limsRunServiceImpl',
            url: limsResult.runUrl + '/queries/raw',
            readonly: true,
            columns: limsResult.getRunGridColumns(),
            requestData: {
                reviewerId_EQ: workspace.user.id,
                status_EQ: 'approve',
                //createdByOrgId_EQ: workspace.user.ext$.loginorgid
            },
            page: true,
            onLoadSuccess: function (rows) {
                if (Gikam.isNotEmpty(rows)) {
                    this.activeRowByIndex(0)
                }
            },
            onRowActive: function (index, row) {//批次的行激活
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BPMN.COMMENT.PASS',
                icon: 'submit', color: 'success',
                onClick: function () {
                    _this.pass();
                }
            }, {
                type: 'button',
                text: 'GIKAM.BPMN.COMMENT.REJECT',
                icon: 'reject',
                onClick: function () {
                    _this.reviewReject();
                }
            },{
                type: 'button',
                text: 'MODULE.BUSINESS.RESULT.BUTTON.HAND_OVER',
                icon: 'export',
                onClick: function () {
                    _this.handOver()
                }
            }],
            onCellClick: function (field, row) {
                field === 'runCode' && row.id && _this.loadRunOrderTaskPage(row)
            },
        }
    },

    handOver: function () {
        var _this = this
        var runListGrid = Gikam.getComp('run-audit-list-grid')
        var selections = runListGrid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
            return
        }
        var testIds =[selections[0].testId]
        selections.forEach(r => {
            if (!testIds.includes(r.testId)) {
                testIds.push(r.testId)
            }
        })
        if (testIds.length > 1) {
            Gikam.alert('MODULE.BUSINESS.RESULT.TIP.ONLY_CHOOSE_ONE_TEST')
            return
        }
        var testerIds = '';
        Gikam.getJsonSync(Gikam.printf(IFM_CONTEXT + '/secure/basemodule/coredata/tests/{id}', {
            id: testIds[0]
        })).done(function (data) {
            testerIds = data.testerId
        })
        Gikam.createLimsModal({
            title: 'T_LIMS_ORDER_TASK.BUTTON.ASSIGNMENT',
            height: '200',
            width: '400',
            onConfirm: function () {
                var form = Gikam.getComp('result-audit-run-handover-form')
                if (form.validate()) {
                    var data = form.getData()
                    if (Gikam.isEmpty(data)) {
                        return
                    }
                    let url = limsResult.runUrl + '/action/result-audit/hand-over'
                    workspace.window.showMask()
                    Gikam.postText(url, Gikam.getJsonWrapper(data, [null, selections])).done(req => {
                        runListGrid.refresh()
                    }).always(function () {
                        workspace.window.closeMask()
                        Gikam.getLastModal().close()
                    })
                }
            },
            m: {
                type: 'form',
                id: 'result-audit-run-handover-form',
                columns: 1,
                titleWidth: 80,
                fields: [{
                    field: 'reviewerId',
                    title: 'T_LIMS_ORDER_TASK.REVIEWER',
                    type: 'select',
                    category: 'user',
                    validators: ['notEmpty'],
                    firstBlank: false,
                    search: true,
                    param: {
                        status_EQ: 'activated',
                        id_IN : testerIds
                    },
                    onChange: function (field, value, oldvalue, index, text) {
                        this.setData({
                            reviewer: text
                        })
                    }
                }],
            }
        })
    },

    pass: function() {
        let grid = Gikam.getComp('run-audit-list-grid');
        let selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
            return;
        }
        Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.SUBMIT', function () {
            workspace.window.showMask();
            Gikam.postText(limsResult.runUrl + '/action/pass',
                Gikam.getJsonWrapper(null, [null, selections])).done(function (result) {
                grid.refresh();
            }).always(function () {
                workspace.window.closeMask();
            });
        })
    },

    reviewReject: function () {
        let grid = Gikam.getComp('run-audit-list-grid');
        let selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
            return;
        }
        Gikam.createLimsModal({
            title: 'MODULE.BUSINESS.RESULT_AUDIT.BUTTON.RETURNBACK',
            height: 220,
            width: 380,
            onConfirm: function () {
                var  form = Gikam.getComp('confirm-form');
                if(form.validate()) {
                    let data = form.getData();
                    workspace.window.showMask();
                    Gikam.postText(limsResult.runUrl + '/action/reject',
                        Gikam.getJsonWrapper(data, [null, selections])).done(function (result) {
                        grid.refresh();
                    }).always(function () {
                        Gikam.getLastModal().close();
                        workspace.window.closeMask();
                    });
                }
            },
            m: {
                type: 'form',
                id: 'confirm-form',
                fields: [{
                    field : 'reason',
                    title : 'T_LIMS_RUN.REVIEWREJECTREASON',
                    validators: ['notEmpty'],
                    type : 'textarea',
                    height: 100,
                }],
                columns: 1,
            }
        })
    },

    loadRunOrderTaskPage: function (row) {
        workspace.window.load(limsResult.baseUrl + '/page/run-order-task-audit-detail' + Gikam.param({
            id: row.id,
            runCode: row.runCode
        }))
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            id : 'lims-result-audit-list-layout',
            renderTo : workspace.window.$dom,
            center: {
                items: [this.getRunGrid()]
            }
        });
    },

    init : function() {
        this.create();
    }
};
